<?php
/**
 * Template Name: Authorise
 */
get_header(); ?>

<?php

$venue = $_REQUEST['form_venue'];
$pitch = $_REQUEST['form_pitch'];
$slot = $_REQUEST['form_slot'];
$date = $_REQUEST['form_date'];
$theuser = $_REQUEST['form_user'];
$price = $_REQUEST['form_price'];
$user_info = get_userdata($theuser);
?>

<div class="container">
    <? while (have_posts()) : the_post(); ?>

        <div>
            <h1>Payment Authorised</h1>

            <div class="row-fluid">
                <div class="span8">
                    <div class="card grey clearfix">
                        <?
                        $flag = false;
                        $q = new WP_Query(array('post_type' => 'booking', 'order' => 'ASC', 'posts_per_page' => 50000));
                        while ($q->have_posts()) : $q->the_post();
                            /*
                            echo get_field('customer_id');
                            echo '<br>';
                            echo get_field('slot');
                            echo '<br>';
                            echo get_field('date');
                            echo '<br>';
                            echo $theuser;
                            echo '<br>';
                            echo $slot;
                            echo '<br>';
                            echo date('Ymd',strtotime($date));
                            echo '<br>';
                            echo $flag;
                            echo '<br>';
                            echo '<br>';
                            echo '<br>';
                            */
                            if (get_field('customer_id') == $theuser && get_field('slot') == $slot && get_field('date') == date('Ymd',strtotime($date))) {
                                $flag = true;
                            }
                        endwhile;
                        wp_reset_postdata();
                        ?>

                        <?if ($flag) { ?>

                            <p>This has already been booked!</p>

                        <? } else { ?>
                        <h2 class="title">Thanks for Booking!</h2>

                        <p>We have emailed you confirmation.</p>
                        <?

                        $data = array(
                            "slot" => $slot,
                            "date" => date('Ymd', strtotime($date)),
                            "customer_id" => $theuser,
                            "email_list" => '',
                            "price" => $price,
                            "authorised" => true
                        );

                        $response = newBooking($data);

                        //echo $response['status'];
                        //var_dump($response);

                        $user_info = get_userdata($theuser);
                        $subject = $user_info->user_firstname . ' Your Football Game Has Been Booked!';
                        $message = '
                            <html>
                            <head>
                              <title></title>
                            </head>
                            <body>
                              <p>Hi ' . $user_info->user_firstname . '!</p>
                              <p>Thanks for booking your game with PitchWise.</p>
                              <p>Your booking is confirmed for ' . date('l, j F Y', strtotime($date)) . ' from ' . get_field('start_time', $slot) . ' until ' . get_field('end_time', $slot) . '</p>
                              <p><strong>Kick-Off</strong>: ' . date('l, j F Y', strtotime($date)) . ' at ' . get_field('start_time', $slot) . '</p>
                                <p><strong>Where</strong>: <a href="' . get_permalink($pitch) . '">' . get_field('pid', $pitch) . '</a> - ' . get_field('name', $venue) . ', ' . get_field('address', $venue) . '</p>
                                <p><strong>Nearest Station(s)</strong>: ' . get_field('nearest_station', $venue) . '</p>
                                <p><strong>Size</strong>: ' . get_field('size', $pitch) . '</p>
                                <p><strong>Surface</strong>: ' . get_field('surface', $pitch) . '</p>
                            </body>
                            </html>';

                        $headers = 'MIME-Version: 1.0' . "\r\n";
                        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                        $headers .= 'To: ' . $user_info->user_firstname . ' <' . $user_info->user_email . '>' . "\r\n";
                        $headers .= 'From: PitchWise <customer@pitchwise.co.uk>' . "\r\n";
                        $headers .= 'Bcc: nazzanuk@gmail.com, nathan@pitchwise.co.uk, avneet@pitchwise.co.uk, kumar@pitchwise.co.uk, customer@pitchwise.co.uk' . "\r\n";
                        //$headers .= 'Bcc: nazzanuk@gmail.com' . "\r\n";

                        mail($to, $subject, $message, $headers);
                        ?>

                        <? } ?>

                        <a class="btn btn-primary" href="/my-account/"
                           style="white-space:nowrap;font-weight: 800; margin-right: 10px;"><i class="icon-user"></i> My
                            Account</a>
                        <a class="btn btn-inverse" href="<? echo get_permalink($response['booking']); ?>"
                           style="white-space:nowrap;font-weight: 800; margin-right: 10px;"><i class="icon-group"></i>
                            MyTeam</a>
                        <a class="btn btn-info" href="" style="white-space:nowrap;font-weight: 800;"><i
                                class="icon-share-alt"></i> Share</a>
                        <a class="btn" href="/pitches/" style="white-space:nowrap;font-weight: 800;"><i
                                class="icon-search"></i> Pitch Search</a>

                    </div>
                </div>
            </div>


        </div>

    <? endwhile; ?>
</div>

<?php get_footer(); ?>
